#include <stdio.h>

#include "MouseHook.h"

MouseHook::MouseHook()
{
	leftButtonLastDown_ = 0;
	hook_handle_ = 0;
	doubleClickTimeAsTicks_ = GetDoubleClickTime() * 10000; // There are 10,000 ticks per millisecond
}

LRESULT CALLBACK LowLevelMouseCallback(int, WPARAM, LPARAM)
{
	return NULL;
}

LRESULT CALLBACK MouseHook::LowLevelMouseCallback_(int nCode, WPARAM wParam, LPARAM lParam)
{
	if(nCode < 0)
		return CallNextHookEx(0, nCode, wParam, lParam);

	printf("Mouse hook callback: %u, %u\n", wParam, lParam);

	MouseEvent eventType;
	short wheelDelta = 0;
	/*
	switch (wParam)
                {
                    case (int)WM_MOUSEMOVE:

                        eventType = Move;
                        break;

                    case (int)WM_LBUTTONDOWN:

                        //printf("click time diff: {0}", DateTime.UtcNow.Ticks - leftButtonLastDown_);

                        eventType = GetTickCount64() - leftButtonLastDown_ < doubleClickTimeAsTicks_ ?
                            LDoubleClick :
                            eventType = LDown;

                        leftButtonLastDown_ = DateTime.UtcNow.Ticks;
                        Debug.WriteLine("set last button down time: {0}", leftButtonLastDown_);

                        break;
                    
                    case (int)WM_LBUTTONUP:

                        eventType = LUp;

                        break;

                    case (int)WM_RBUTTONDOWN:

                        eventType = RDown;
                        break;

                    case (int)WM_RBUTTONUP:

                        eventType = RUp;
                        break;

                    case (int)WM_MOUSEWHEEL:

                        eventType = Wheel;
                        wheelDelta = (short)(ll.mouseData >> 16);
                        break;
                }
				
                if(eventType != 0)
					MouseEventCallback(eventType, ll.time, (short)ll.pt.X, (short)ll.pt.Y, wheelDelta);
                    //MouseEventOccured(eventType, ll.time, (short)ll.pt.X, (short)ll.pt.Y, wheelDelta);
					*/
                // Return non-zero to prevent actions from reaching next procedure
                //if (!CallNextHook)
                //    return 1;

                return CallNextHookEx(0, nCode, wParam, lParam);
}

bool MouseHook::Start()
{
	//mouse_.usUsagePage = USAGE_PAGE_STD_INPUT;
	//mouse_.usUsage = USAGE_MOUSE;
	//mouse_.dwFlags = RIDEV_INPUTSINK;

	//RegisterRawInputDevices(&mouse_, 1, 

	return true;
}